package com.liqi.frame.comm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.liqi.frame.comm.dao.PermissionDao;
import com.liqi.frame.comm.dto.MenuDto;
import com.liqi.frame.comm.enums.PermissionTypeEnum;
import com.liqi.frame.comm.po.Permission;
import com.liqi.frame.comm.service.MenuService;
import com.liqi.frame.comm.service.PermissionService;
import com.liqi.frame.comm.token.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liqi on 2017/12/20.
 * liqiwork@qq.com
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Override
    public List<MenuDto> getMenuListWithName(String menuName) {
        List<Permission> list = permissionDao.getPermissionListWithName(menuName, PermissionTypeEnum.MENU.getKey());
        MenuDto menu = null;
        List<MenuDto> menuList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            menu = new MenuDto();
            BeanUtil.copyProperties(list.get(i), menu);
            menu.setMenuKey(list.get(i).getPermissionKey());
            menu.setMenuName(list.get(i).getPermissionName());
            menuList.add(menu);
        }
        return menuList;
    }


    @Override
    public List<MenuDto> getMenuListByKey(String menuKey) {
        List<Permission> list = permissionDao.getPermissionsByKey(menuKey, PermissionTypeEnum.MENU.getKey());
        MenuDto menu = null;
        List<MenuDto> menuList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            menu = new MenuDto();
            BeanUtil.copyProperties(list.get(i), menu);
            menu.setMenuKey(list.get(i).getPermissionKey());
            menu.setMenuName(list.get(i).getPermissionName());
            menuList.add(menu);
        }
        return menuList;
    }

    @Override
    public List<MenuDto> getMenuList() {
            List<Permission> list =  permissionDao.getMenuList();
        MenuDto menu = null;
        List<MenuDto> menuList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            menu = new MenuDto();
            BeanUtil.copyProperties(list.get(i), menu);
            menu.setMenuKey(list.get(i).getPermissionKey());
            menu.setMenuName(list.get(i).getPermissionName());
            menuList.add(menu);
        }
        return menuList;
    }

    @Override
    public List<MenuDto> getMenuListByCurrUser() {
        List<Permission> list = new ArrayList<>();
        if (TokenUtil.isAdministrator()) {
            list = permissionDao.getMenuList();
        } else {
            list = permissionDao.getMenuListByUserId(TokenUtil.getUserId());
        }
        MenuDto menu = null;
        List<MenuDto> menuList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            menu = new MenuDto();
            BeanUtil.copyProperties(list.get(i), menu);
            menu.setMenuKey(list.get(i).getPermissionKey());
            menu.setMenuName(list.get(i).getPermissionName());
            menuList.add(menu);
        }
        return menuList;
    }


    @Override
    public List<MenuDto> getMenuListByUserId(String userId) {
        List<Permission> list =  permissionDao.getMenuListByUserId(userId);
        MenuDto menu = null;
        List<MenuDto> menuList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            menu = new MenuDto();
            BeanUtil.copyProperties(list.get(i), menu);
            menu.setMenuKey(list.get(i).getPermissionKey());
            menu.setMenuName(list.get(i).getPermissionName());
            menuList.add(menu);
        }
        return menuList;
    }

    @Override
    public List<MenuDto> getMenuListByUsername(String username) {
        List<Permission> list =  permissionDao.getMenuListByUsername(username);
        MenuDto menu = null;
        List<MenuDto> menuList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            menu = new MenuDto();
            BeanUtil.copyProperties(list.get(i), menu);
            menu.setMenuKey(list.get(i).getPermissionKey());
            menu.setMenuName(list.get(i).getPermissionName());
            menuList.add(menu);
        }
        return menuList;
    }

    @Override
    public List<MenuDto> getMenuListByRoleId(String roleId) {
        List<Permission> list =  permissionDao.getMenuListByRoleId(roleId);
        MenuDto menu = null;
        List<MenuDto> menuList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            menu = new MenuDto();
            BeanUtil.copyProperties(list.get(i), menu);
            menu.setMenuKey(list.get(i).getPermissionKey());
            menu.setMenuName(list.get(i).getPermissionName());
            menuList.add(menu);
        }
        return menuList;
    }

    @Override
    public MenuDto getMenuById(String id) {
        Permission permission = permissionDao.getPermissionById(id);
        MenuDto menu = new MenuDto();
        BeanUtil.copyProperties(permission, menu);
        menu.setMenuKey(permission.getPermissionKey());
        menu.setMenuName(permission.getPermissionName());
        return menu;
    }

    @Override
    public int insertMenu(String id, String menuKey, String menuName, String parentId, Integer level, String comment, Float orderId, String target, String href) {
        return permissionService.insertPermission(id, menuKey, menuName, PermissionTypeEnum.MENU.getKey(), parentId, level, comment, orderId, target, href);
    }

    @Override
    public int updateMenu(String id, String menuKey, String menuName, String parentId, Integer level, String comment, Float orderId, String target, String href) {
        return permissionService.updatePermission(id, menuKey, menuName, null, parentId, level, comment, orderId, target, href);
    }

    @Override
    public int deleteMenuById(String id) {
        return permissionService.deletePermissionById(id);
    }

    @Override
    public int deleteMenuListByParentId(String parentId) {
        return permissionService.deleteMenuListByParentId(parentId);
    }

    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private PermissionService permissionService;
}
